package com.miracle.common.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.miracle.common.R;
import com.miracle.common.bean.User.CheckBean;

import java.util.List;

/**
 * 单选
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/6/19 14:04
 */
public class SingleSelectAdapter extends BaseQuickAdapter<CheckBean, BaseViewHolder> {
    List<CheckBean> mList;
    OnSingleItemClick singleClickListener;

    public SingleSelectAdapter(@LayoutRes int layoutResId, @Nullable List<CheckBean
            > data) {
        super(layoutResId, data);
        mList = data;

    }

    @Override
    protected void convert(final BaseViewHolder helper, final CheckBean item) {
        TextView textView = helper.getView(R.id.tv);
        CheckBox checkBox = helper.getView(R.id.cb);

        checkBox.setChecked(item.isChecked());
        textView.setText(item.getText());
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = helper.getLayoutPosition();
                for (CheckBean bean : mList) {
                    bean.setChecked(false);
                }
                mList.get(position).setChecked(true);
                notifyDataSetChanged();
                singleClickListener.onSingleClick(position);
            }
        });
    }

    public void setSingleClickListener(OnSingleItemClick listener) {
        singleClickListener = listener;
    }

    /**
     * 点击
     */
    public interface OnSingleItemClick {
        /**
         * 选中点击事件
         * @param position
         */
        void onSingleClick(int position);
    }
}
